<?php

class Store_ShareController extends YouNet_Controller_Action
{
	public function productAction()
	{
		$id_product		= $this->_getParam('id_product');
		$id_page		= $this->_getParam('id_page');
		$id_user_ref	= $this->_getParam('id_user_ref');

		if( Core_Util::isFacebookRequest() )
		{
			if( !empty($id_product) )
			{
				$productTbl = new Core_Model_DbTable_Products();
				$product	= $productTbl->getShareProduct($id_product);
				
				$this->view->admins			= $product->getStore()->getCreator()->id_social;
				$this->view->is_facebook	= true;
				$this->view->product		= $product;
                $this->view->share_title	= sprintf('%s : %s', $product->getStore()->getName(), $product->getName());
                $this->view->share_url		= $product->getShareUrl(array('id_page' => $id_page));
			}
		}
		else
		{
			if( !empty($id_product) && !empty($id_page) )
			{
				$this->disableAutoRender();
				Core_Util::parentRedirect($this->_social->pageUrl(array('target' => 'detail', 'id_product' => $id_product), $id_page));
			}
			else
			{
				throw new Exception('Page Not Found', 404);
			}
		}
	}
	
	public function storeAction()
	{
		$id_page		= $this->_getParam('id_page');
		$id_store		= $this->_getParam('id_store');
		if( Core_Util::isFacebookRequest() )
		{
			if( !empty($id_store) && !empty($id_page) )
			{
				$storeTbl = new Core_Model_DbTable_Stores();
				$store	= $storeTbl->getShareStore($id_store, $id_page);
				$this->view->is_facebook		= true;
				$this->view->store				= $store;
                $this->view->share_title		= sprintf("%s : %s", $store->getPageName(), $store->getName());
                $this->view->share_url			= $this->_social->getShareUrl('/store/share/store/id_store/' . $store->id . '/id_page/' . $id_page);
				$banner_thumb_url = $store->getBannerThumbImageUrl();
				if (empty($store->banner_image))
				{
					$banner_thumb_url = STATIC_URL . '/img/logo_200x.png';
				}
				$this->view->banner_thumb_url = $banner_thumb_url;
			}
		}
		else
		{
			if( !empty($id_store) && !empty($id_page) )
			{
				$this->_social->redirectLink($this->_social->pageUrl(array(), $id_page));
			}
			else
			{
				throw new Exception('Page Not Found', 404);
			}
		}
	}
}
